<?php
class MSubscriber extends CI_Model{

	public function __construct()
    {
        parent::__construct();
    }

	function getSubscriberById($id) {
		$data = array();
	    $this->db->where('subscriber_id',id_clean($id));
	    $this->db->limit(1);
	    $Q = $this->db->get('subscriber');
	    if ($Q->num_rows() > 0){
	      $data = $Q->row_array();
	    }
	
	    $Q->free_result();    
	    return $data;      
	}
    
    function getSubscriberList()
    {
         $data = array();
         $this->db->order_by("email");
	     $query = $this->db->get('subscriber');
         return $query->result();
    }
    
    function getTemplateList()
    {
         $data = array();
         $this->db->order_by("subject");
	     $query = $this->db->get('emailtemplate');
         return $query->result();
    }
	
	function addSubscriber(){
        $this->db->set('email', db_clean($_POST['subscriber']));
    	$this->db->set('subscribed_date', 'NOW()', FALSE);
    	$this->db->insert('Subscriber');
	}
    
    function addFeedback(){
        $data = array(
                    'name' => db_clean($_POST['name']),
                    'email' => db_clean($_POST['email']),
                    'subject' => db_clean($_POST['subject']),
                    'message' => $_POST['message']
                );
    	$this->db->set('cdate', 'NOW()', FALSE);
    	$this->db->insert('feedback',$data);
	}
    
    function addGeoVisitor(){
        $vIP = $_SERVER['REMOTE_ADDR'];
        $data = array(
                    //'ipaddr' => db_clean($_POST['ip']),
                    'ipaddr' => db_clean($vIP),
                    'countrycode' => db_clean($_POST['ccode']),
                    'countryname' => db_clean($_POST['cname']),
                    'regionname' => db_clean($_POST['regname']),
                    'cityname' => db_clean($_POST['cityname']),
                    'zipcode' => db_clean($_POST['zipcode']),
                    'latitude' => db_clean($_POST['lat']),
                    'longitude' => db_clean($_POST['longi']),
                    'timezone' => db_clean($_POST['timezone'])    
                );
        if($this->validateVisitor($vIP)==0){
            $this->db->set('datevisited', 'NOW()', FALSE);
	        $this->db->insert('trackvisitor',$data);
        }
	}
	
    function validateVisitor($ip)
    {
        $sql = "SELECT count(ipaddr) as cnt FROM trackvisitor WHERE ipaddr='".$ip."' AND curdate()=date(datevisited)";
        $query = $this->db->query($sql); 
        $row = $query->row_array();
        return  $row['cnt'];   
    }
    
    function gettodayVisitor()
    {
        $sql = "SELECT count(ipaddr) as cnt FROM trackvisitor WHERE curdate()=date(datevisited)";
        $query = $this->db->query($sql); 
        $row = $query->row_array();
        return  $row['cnt'];   
    }
    
    function gettotalVisitor()
    {
        $sql = "SELECT count(ipaddr) as cnt FROM trackvisitor";
        $query = $this->db->query($sql); 
        $row = $query->row_array();
        return  $row['cnt'];   
    }
    
	function deleteSubscriber($id){
 	    $this->db->where('Subscriber_id', id_clean($id));
        $this->db->delete('Subscriber');
	}
    
    function addEditTemplate($id = 0)
    {
        $data = array('subject' => db_clean($_POST['subject']),
                    'body' => $_POST['emailbody'],
                    'activate' => db_clean(isset($_POST['activate']) ? 1 : 0)
                    );
        
       if($id > 0)
       {
           $this->db->where('template_id',id_clean($id));
    	   $this->db->update('emailtemplate',$data);
       }
       else
       {
    	   $this->db->insert('emailtemplate',$data);
       }
    }
    
    function getTemplateById($id) {
		$data = array();
	    $this->db->where('template_id',id_clean($id));
	    $this->db->limit(1);
	    $Q = $this->db->get('emailtemplate');
	    if ($Q->num_rows() > 0){
	      $data = $Q->row_array();
	    }
	
	    $Q->free_result();    
	    return $data;      
	}
    
}
?>